import time
from email.mime.application import MIMEApplication
import requests
import json
import xlrd
import hashlib
import xlutils.copy
import xlwt
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import datetime
import random


class test(object):
    def __init__(self):
        self.timestamp = None
        self.check_code = None
        self.wb = None
        self.stn = None
        self.style = None
        self.token = None
        self.url = None
        self.headers = None
        self.idNum = None
        self.params = None
        self.expectResult = None
        self.auth_token = None
        self.order_no = None
        self.uid = None
        self.amount = None
        self.order_id = None
        self.device_type = None
        self.n = 0
        self.m = 0
        self.faceid = None

    def generateMd5(self):
        self.timestamp = time.time()
        a = str(self.timestamp) + 'CMJF_getSignInfo'
        md5 = hashlib.md5()
        md5.update(a.encode('utf-8'))
        self.check_code = md5.hexdigest()

    def readExcel(self, num, one):
        # 打开当前目录中的测试用例文档，获取sheet页
        workBook = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\零售硬件接口自动化测试用例.xls", formatting_info=True)
        sheetName = workBook.sheet_names()
        self.wb = xlutils.copy.copy(workBook)
        self.stn = self.wb.get_sheet(sheetName[num])
        # print(self.stn)
        self.style = xlwt.XFStyle()
        alignment = xlwt.Alignment()
        font = xlwt.Font()
        font.name = u'微软雅黑'
        borders = xlwt.Borders()
        # pattern = xlwt.Pattern()
        alignment.horz = 0x02
        alignment.vert = 0x01
        alignment.wrap = 1
        borders.left = xlwt.Borders.THIN  # 添加边框-虚线边框
        borders.right = xlwt.Borders.THIN  # 添加边框-虚线边框
        borders.top = xlwt.Borders.THIN  # 添加边框-虚线边框
        borders.bottom = xlwt.Borders.THIN  # 添加边框-虚线边框
        # pattern.pattern_fore_colour = 0x0A
        self.style.alignment = alignment
        self.style.borders = borders
        self.style.font = font
        # print(f"{i}线程执行了")
        # 按行，列定位excel，且取出对应的参数（url，header，data）作为请求参数
        # data, sheetName = t.readExcel(0)
        sheet_name = workBook.sheet_by_name(sheetName[num])
        self.url = sheet_name.cell(one, 4).value
        self.headers = eval(sheet_name.cell(one, 6).value)
        self.idNum = str(sheet_name.cell(one, 0).value)
        self.params = eval(sheet_name.cell(one, 7).value)
        self.expectResult = json.loads(sheet_name.cell(one, 9).value)

        # print(self.url)
        # print(self.headers)
        # print(self.idNum)
        # print(self.params)
        # print(self.expectResult)
        # return workBook, sheetName

    def writeExcel(self, nums, r2, r3, one):
        if nums == 1:
            if r2['code'] == self.expectResult['code']:
                print(f'编号{self.idNum}：用例测试通过！')
                self.n += 1
                self.stn.write(one, 10, r3, self.style)
                self.stn.write(one, 11, "PASS", self.style)
            else:
                self.m += 1
                print(f'编号{self.idNum}：用例测试失败！')
                self.stn.write(one, 10, r3, self.style)
                self.stn.write(one, 11, "FAIL", self.style)
        if nums == 2:
            self.m += 1
            self.stn.write(one, 10, "", self.style)
            self.stn.write(one, 11, "", self.style)

    def get_token(self):  # 获取激活接口返回的token，保存为self.token
        t.generateMd5()
        # url1 = 'http://open.dairphone.cn/device/status'
        url1 = 'https://retail-device.triumen.cn/device/check_status'
        data1 = {
            "sn": "SMIT3B2019816000090",
            "sign": self.check_code,
            "time": self.timestamp,
            "version": "3.4.0"
        }
        result1 = requests.post(url1, data=data1, headers=self.headers)
        results1 = result1.json()
        print(results1)
        try:
            self.token = results1['data']['token']
            self.uid = results1['data']['uid']
        except Exception:
            print(Exception)
            print(json.dumps(results1, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        print(self.token)

    def get_auth_token(self, account, password, role_login):  # 获取登录接口返回的auth_token，保存为self.auth_token
        url2 = 'https://open.dairphone.cn/jfdevice/login?ver=1.0&from=hardware'
        data2 = {
            "sn": "SMIT3B2019816000090",
            "account": account,
            "password": password,
            "role_login": role_login,
            "token": self.token  # 取激活接口返回的token用作登录，目的是拿到返回的auth_token
        }
        result2 = requests.post(url2, data=data2, headers=self.headers)
        results2 = result2.json()
        try:
            self.auth_token = results2['data']['auth_token']
        except Exception:
            print(Exception)
            print(json.dumps(results2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        # print(self.auth_token)

    def get_order_no(self, auth_code):
        t.get_token()
        # auth_code = str(input("在此输入微信/支付宝付款条码：\n"))
        url3 = 'http://open.dairphone.cn/device/back_scanning'
        data3 = {
            "sn": "SMIT3B2019816000090",
            "price": 1,
            "auth_code": auth_code,
            "token": self.token,
            "version": "3.4.0"
        }
        result3 = requests.post(url3, data=data3, headers=self.headers)
        results3 = result3.json()
        self.order_no = results3['data']['order_no']

    def get_amount(self):
        t.get_token()
        t.get_auth_token("17620533205", "123456", "0")
        date = datetime.datetime.now().strftime("%Y-%m-%d")
        url4 = 'https://open.dairphone.cn/jfdevice/getorderlist?ver=1.0&from=hardware'
        data4 = {
            "sn": "SMIT3B2019816000090",
            "token": self.token,
            "date": date,
            "page": 1,
            "auth_token": self.auth_token,
            "version": "3.4.0"
        }
        result4 = requests.post(url4, data=data4, headers=self.headers)
        results4 = result4.json()
        self.order_id = results4['data']['list'][0]['id']
        self.amount = results4['data']['list'][0]['pay_price']
        # print(self.order_id)
        # print(self.amount)

    def get_type(self):
        t.generateMd5()
        url5 = "http://open.dairphone.cn/device/config_list"
        data5 = {
            "check_code": self.check_code,
            "time": self.timestamp,
            "sn": "SMIT3B2019816000090",
            "version": "3.4.0"
        }
        result5 = requests.post(url5, data=data5, headers=self.headers)
        results5 = result5.json()
        self.device_type = results5['data']['device_type']

    def get_faceid(self):
        t.get_token()
        t.get_auth_token("17620533205", "123456", "0")
        url6 = "https://open.dairphone.cn/jfdevice/getbindopenidlist?ver=1.0&from=hardware"
        data6 = {
            "token": self.token,
            "auth_token": self.auth_token,
            "sn": "SMIT3B2019816000090",
            "version": "3.4.0"
        }
        result6 = requests.post(url6, data=data6, headers=self.headers)
        results6 = result6.json()
        self.faceid = results6['data']['list'][0]['id']

    def config(self, one):
        t.readExcel(0, one)  # 调用excel，获取表格-sheet-行列中的header、url、params
        t.generateMd5()  # 调用生成MD5函数，用作生成check_code
        #  对表格中参数中字段和参数的判断，如果有，则不替换，如果有字段，空参数，则添加，无字段则忽略
        if 'check_code' in self.params:
            if self.params['check_code'] == '1234567890':
                pass
            if self.params['check_code'] == '':
                self.params['check_code'] = self.check_code
        if 'time' in self.params:
            if self.params['time'] == '1234567890':
                pass
            if self.params['time'] == '':
                self.params['time'] = self.timestamp
        # 开始提交参数到接口
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        # 取表格中预期结果中的key和对应的value，用来判断测试的结果是否一致，如果是，则PASS，且写入表格结果栏中，否则FAIL
        t.writeExcel(r2, r3, one)
        one += 1

    def status(self, one):
        t.readExcel(1, one)  # 调用excel，获取表格-sheet-行列中的header、url、params
        #  对表格中参数中字段和参数的判断，如果有，则不替换，如果有字段，空参数，则添加，无字段则忽略
        if 'sign' in self.params:
            if self.params['sign'] == '1234567890':
                pass
            if self.params['sign'] == '':
                self.params['sign'] = self.check_code
        if 'time' in self.params:
            if self.params['time'] == '1234567890':
                pass
            if self.params['time'] == '':
                self.params['time'] = self.timestamp
        # 开始提交参数到接口
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        # 取表格中预期结果中的key和对应的value，用来判断测试的结果是否一致，如果是，则PASS，且写入表格结果栏中，否则FAIL
        t.writeExcel(r2, r3, one)
        one += 1

    def wx_auth_info(self, one):
        t.readExcel(2, one)
        t.get_token()  # 调用函数，获取token
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        # 取表格中预期结果中的key和对应的value，用来判断测试的结果是否一致，如果是，则PASS，且写入表格结果栏中，否则FAIL
        t.writeExcel(r2, r3, one)
        one += 1

    def back_sweep(self, one, auth_code):
        t.readExcel(3, one)
        t.get_token()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'auth_code' in self.params:
            if self.params['auth_code'] == '1234567890':
                pass
            if self.params['auth_code'] == '134537232597573158':  # 测试反扫错误码时可在用例及这里追加各种错误码的auth_code
                pass
            if self.params['auth_code'] == '':
                self.params['auth_code'] = auth_code
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        # 取表格中预期结果中的key和对应的value，用来判断测试的结果是否一致，如果是，则PASS，且写入表格结果栏中，否则FAIL
        t.writeExcel(r2, r3, one)
        one += 1

    def order(self, one, auth_code):
        t.readExcel(4, one)
        t.get_token()
        t.get_order_no(auth_code)
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'order_no' in self.params:
            if self.params['order_no'] == '1234567890':
                pass
            if self.params['order_no'] == '':
                self.params['order_no'] = self.order_no
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def check(self, one):
        t.readExcel(5, one)
        t.get_token()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def update(self, one):
        t.readExcel(6, one)
        t.get_type()
        if 'type' in self.params:
            if self.params['type'] == '1234567890':
                pass
            if self.params['type'] == '':
                self.params['type'] = self.device_type
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def log(self, one):
        t.readExcel(7, one)
        t.get_token()
        if 'type' in self.params:
            if self.params['uid'] == '1234567890':
                pass
            if self.params['uid'] == '':
                self.params['uid'] = self.uid
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def on_duty(self, one):
        t.readExcel(8, one)
        t.get_token()
        t.get_auth_token("13148845668", "123456", "1")
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'auth_token' in self.params:
            if self.params['auth_token'] == '1234567890':
                pass
            if self.params['auth_token'] == '':
                self.params['auth_token'] = self.auth_token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def duty_record(self, one):
        t.readExcel(9, one)
        t.get_token()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'day' in self.params:
            if self.params['day'] == '1234567890':
                pass
            if self.params['day'] == '':
                self.params['day'] = self.timestamp
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def handover(self, one):
        t.readExcel(10, one)
        t.get_token()
        t.get_auth_token("17620533205", "123456", "0")
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'day' in self.params:
            if self.params['day'] == '1234567890':
                pass
            if self.params['day'] == '':
                self.params['day'] = self.timestamp
        if 'auth_token' in self.params:
            if self.params['auth_token'] == '1234567890':
                pass
            if self.params['auth_token'] == '':
                self.params['auth_token'] = self.auth_token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def static_code(self, one):
        t.generateMd5()
        t.readExcel(11, one)
        t.get_token()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'time' in self.params:
            if self.params['time'] == '1234567890':
                pass
            if self.params['time'] == '':
                self.params['time'] = self.timestamp
        r1 = requests.get(self.url, params=self.params, headers=self.headers)
        r2 = r1.status_code
        if r2 == 200:
            print(f'编号{self.idNum}：用例测试通过！')
            self.stn.write(one, 10, r2, self.style)
            self.stn.write(one, 11, "PASS", self.style)
        else:
            print(f'编号{self.idNum}：用例测试失败！')
            self.stn.write(one, 10, r2, self.style)
            self.stn.write(one, 11, "FAIL", self.style)
        one += 1

    def login(self, one):
        t.readExcel(12, one)
        t.get_token()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def order_list(self, one):
        t.readExcel(16, one)
        t.get_token()
        t.get_auth_token("17620533205", "123456", "0")
        date = datetime.datetime.now().strftime("%Y-%m-%d")
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'auth_token' in self.params:
            if self.params['auth_token'] == '1234567890':
                pass
            if self.params['auth_token'] == '':
                self.params['auth_token'] = self.auth_token
        if 'date' in self.params:
            if self.params['date'] == '1234567890':
                pass
            if self.params['date'] == '':
                self.params['date'] = date
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def refund(self, one):
        t.readExcel(14, one)
        t.get_token()
        t.get_auth_token("17620533205", "123456", "0")
        t.get_amount()
        if 'amount' in self.params:
            if self.params['amount'] == '1234567890':
                pass
            if self.params['amount'] == '':
                self.params['amount'] = self.amount
        if 'uid' in self.params:
            if self.params['uid'] == '1234567890':
                pass
            if self.params['uid'] == '':
                self.params['uid'] = self.uid
        if 'order_no' in self.params:
            if self.params['order_no'] == '1234567890':
                pass
            if self.params['order_no'] == '':
                self.params['order_no'] = self.order_id
        if 'auth_token' in self.params:
            if self.params['auth_token'] == '1234567890':
                pass
            if self.params['auth_token'] == '':
                self.params['auth_token'] = self.auth_token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def card_code(self, one):
        t.generateMd5()
        t.readExcel(15, one)
        t.get_token()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'time' in self.params:
            if self.params['time'] == '1234567890':
                pass
            if self.params['time'] == '':
                self.params['time'] = self.timestamp
        r1 = requests.get(self.url, params=self.params)
        r2 = r1.status_code
        if r2 == 200:
            print(f'编号{self.idNum}：用例测试通过！')
            self.stn.write(one, 10, r2, self.style)
            self.stn.write(one, 11, "PASS", self.style)
        else:
            print(f'编号{self.idNum}：用例测试失败！')
            self.stn.write(one, 10, r2, self.style)
            self.stn.write(one, 11, "FAIL", self.style)
        one += 1

    def hb_staging(self, one):
        price = str(random.randint(1, 10))
        t.readExcel(16, one)
        t.get_token()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'price' in self.params:
            if self.params['price'] == '0':
                pass
            if self.params['price'] == '':
                self.params['price'] = price
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def login_human_face(self, one):
        t.readExcel(17, one)
        t.get_token()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def login_list_face(self, one):
        t.readExcel(18, one)
        t.get_token()
        t.get_auth_token("17620533205", "123456", "0")
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'auth_token' in self.params:
            if self.params['auth_token'] == '1234567890':
                pass
            if self.params['auth_token'] == '':
                self.params['auth_token'] = self.auth_token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def add_human_face(self, one):  # nickname未知，不确定来源
        t.readExcel(19, one)
        t.get_token()
        t.get_auth_token("17620533205", "123456", "0")
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'auth_token' in self.params:
            if self.params['auth_token'] == '1234567890':
                pass
            if self.params['auth_token'] == '':
                self.params['auth_token'] = self.auth_token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def delete_human_face(self, one):  # id未知，不确定来源
        t.readExcel(20, one)
        t.get_token()
        t.get_auth_token("17620533205", "123456", "0")
        t.get_faceid()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'auth_token' in self.params:
            if self.params['auth_token'] == '1234567890':
                pass
            if self.params['auth_token'] == '':
                self.params['auth_token'] = self.auth_token
        if 'id' in self.params:
            if self.params['id'] == '1234567890':
                pass
            if self.params['id'] == '':
                self.params['id'] = self.faceid
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def password_verification(self, one):
        t.readExcel(21, one)
        t.get_token()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def refund_query(self, one):
        t.readExcel(22, one)
        t.get_auth_token("17620533205", "123456", "0")
        t.get_amount()
        if 'order_no' in self.params:
            if self.params['order_no'] == '1234567890':
                pass
            if self.params['order_no'] == '':
                self.params['order_no'] = self.order_id
        if 'auth_token' in self.params:
            if self.params['auth_token'] == '1234567890':
                pass
            if self.params['auth_token'] == '':
                self.params['auth_token'] = self.auth_token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def refund_qrcode(self, one):  # id未知，不确定来源
        t.readExcel(23, one)
        t.get_token()
        t.get_auth_token("17620533205", "123456", "0")
        t.get_amount()
        if 'order_no' in self.params:
            if self.params['order_no'] == '1234567890':
                pass
            if self.params['order_no'] == '':
                self.params['order_no'] = self.order_id
        if 'auth_token' in self.params:
            if self.params['auth_token'] == '1234567890':
                pass
            if self.params['auth_token'] == '':
                self.params['auth_token'] = self.auth_token
        if 'uid' in self.params:
            if self.params['uid'] == '1234567890':
                pass
            if self.params['uid'] == '':
                self.params['uid'] = self.uid
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def newest_order(self, one):  # id未知，不确定来源
        t.readExcel(24, one)
        t.get_auth_token("17620533205", "123456", "0")
        if 'auth_token' in self.params:
            if self.params['auth_token'] == '1234567890':
                pass
            if self.params['auth_token'] == '':
                self.params['auth_token'] = self.auth_token
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def flower_installment_payment(self, one):
        price = str(random.randint(1, 10))
        t.readExcel(25, one)
        t.get_token()
        if 'token' in self.params:
            if self.params['token'] == '1234567890':
                pass
            if self.params['token'] == '':
                self.params['token'] = self.token
        if 'price' in self.params:
            if self.params['price'] == '0':
                pass
            if self.params['price'] == '':
                self.params['price'] = price
        r1 = requests.post(self.url, data=self.params, headers=self.headers)
        r2 = r1.json()
        r3 = json.dumps(r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False)
        t.writeExcel(r2, r3, one)
        one += 1

    def save(self):
        # 保存修改，不保存则写入的全部无效
        self.wb.save(r"C:\Users\Administrator\Desktop\零售硬件接口自动化测试用例.xls")

    @staticmethod
    def sendEmail():
        smtpserver = "smtp.qq.com"
        smtpport = 465
        from_mail = "1520609897@qq.com"
        to_mail = ["1520609897@qq.com"]
        password = "whywgtsyqjvjbafe"  # 16位授权码
        subject = "接口测试报告"
        from_name = "王智景"
        msg = MIMEMultipart()
        msg["Subject"] = Header(subject, "utf-8")
        msg["From"] = Header(from_name + " <" + from_mail + ">", "utf-8")
        msg["To"] = Header(",".join(to_mail), "utf-8")
        body = f"共有{t.n}条用例测试通过！\n共有{t.m}条用例测试失败，请打开Excel查看缘由！"
        msgtext = MIMEText(body, "plain", "utf-8")
        xlsxpart = MIMEApplication(open(r"C:\Users\Administrator\Desktop\零售硬件接口自动化测试用例.xls", 'rb').read())
        basename = "接口自动化测试用例.xls"
        xlsxpart.add_header('Content-Disposition', 'attachment', filename=('gbk', '', basename))
        msg.attach(msgtext)
        msg.attach(xlsxpart)
        ret = True
        try:
            server = smtplib.SMTP_SSL(smtpserver, smtpport)
            server.login(from_mail, password)
            server.sendmail(from_mail, to_mail, msg.as_string())
            server.quit()
        except(smtplib.SMTPException) as e:
            ret = False
            print(e.message)
        return ret

    @staticmethod
    def get_sheet():
        workBooks = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\零售硬件接口自动化测试用例.xls", formatting_info=True)
        sheetNames = workBooks.sheet_names()  # 打印所有sheet名
        m = int(len(sheetNames))  # 获取一张表格中有多少sheet
        i = 0
        for j in sheetNames:
            m -= 1
            row = workBooks.sheet_by_name(j).nrows
            print(f"sheet{i + 1}：{sheetNames[i]}有{row}行")
            i += 1


# t = test()  # 实例化对象
# t.get_sheet()  # 调用函数，获取所有sheet页的名称及行数
# numss = int(input("接口测试输入1，清空excel返回数据和结果输入2：\n"))
# if numss == 1:
#     try:
#         for nums in range(1, 12):
#             t.config(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#
#     else:
#         time.sleep(3)
#
#     try:
#         for nums in range(1, 10):
#             t.status(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#
#     else:
#         time.sleep(3)
#
#     # try:
#     #     for nums in range(1, 10):
#     #         t.wx_auth_info(nums)
#     #         nums += 1
#     #         t.save()
#     # except Exception:
#     #     print(Exception)
#     # else:
#     #     time.sleep(3)
#     try:
#         back_sweep_auth_code = str(input("反扫接口：在此输入微信/支付宝付款条码：\n"))
#         for nums in range(1, 14):
#             t.back_sweep(nums, back_sweep_auth_code)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         order_auth_code = str(input("获取订单号：在此输入微信/支付宝付款条码：\n"))
#         for nums in range(1, 10):
#             t.order(nums, order_auth_code)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 8):
#             t.check(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 8):
#             t.update(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 10):
#             t.log(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     # try:
#     #     for nums in range(1, 13):
#     #         t.on_duty(nums)
#     #         nums += 1
#     #         t.save()
#     # except Exception:
#     #     print(Exception)
#     # else:
#     #     time.sleep(3)
#     try:
#         for nums in range(1, 10):
#             t.duty_record(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 12):
#             t.handover(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 16):
#             t.static_code(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 14):
#             t.login(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 14):
#             t.order_list(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 14):
#             t.refund(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 8):
#             t.card_code(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 8):
#             t.login_list_face(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 12):
#             t.add_human_face(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 10):
#             t.delete_human_face(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     # try:
#     #     for nums in range(1, 10):
#     #         t.password_verification(nums)
#     #         nums += 1
#     #         t.save()
#     # except Exception:
#     #     print(Exception)
#     # else:
#     #     time.sleep(3)
#     try:
#         for nums in range(1, 10):
#             t.refund_query(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 12):
#             t.refund_qrcode(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 10):
#             t.newest_order(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     try:
#         for nums in range(1, 8):
#             t.flower_installment_payment(nums)
#             nums += 1
#             t.save()
#     except Exception:
#         print(Exception)
#     else:
#         time.sleep(3)
#     print(f"共有{t.n}条用例测试通过！")
#     print(f"共有{t.m}条用例测试失败，请打开Excel查看具体原因！")
#     # ret = t.sendEmail()
#     # if ret:
#     #     print("邮件发送成功！")
#     # else:
#     #     print("邮件发送失败！")
#     # print("60秒后会自动关闭该界面")
#     time.sleep(60)
#     # 开启线程
#     # for num in range(500):
#     #     t1 = threading.Thread(target=t.getUrl, args=(x, f"t{num + 1}",))
#     #     t1.start()
#     #     t1.join()
# elif numss == 2:
#     workBooks = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\零售硬件接口自动化测试用例.xls", formatting_info=True)
#     sheetNames = workBooks.sheet_names()  # 打印所有sheet名
#     m = int(len(sheetNames))  # 获取一张表格中有多少sheet
#     i = 0
#     for j in sheetNames:
#         m -= 1
#         row = workBooks.sheet_by_name(j).nrows
#         print(f"sheet{i + 1}：{sheetNames[i]}有{row}行")
#         t.readExcel(i, row)
#         t.writeExcel(numss, r2=None, r3=None, one=row)
#         i += 1
# else:
#     print("输入错误！")
